
const HeroInfoData = [
    {"nickName":"及时雨","heroName":"宋江","id":0,"level":30,"blood":0.8,"intro":"原为山东郓城县押司，身材矮小，面目黝黑，为梁山起义军领袖，在一百单八将中稳坐梁山泊第一把交椅"},
    {"nickName":"玉麒麟","heroName":"卢俊义","id":1,"level":30,"blood":0.15,"intro":"武艺高强，棍棒天下无双，江湖人称河北三绝"},
    {"nickName":"智多星","heroName":"吴用","id":2,"level":30,"blood":0.20,"intro":"满腹经纶，通晓文韬武略，足智多谋，常以诸葛亮自比"},
    {"nickName":"入云龙","heroName":"公孙胜","id":3,"level":30,"blood":0.89,"intro":"他与晁盖、吴用等七人结义，一同劫取生辰纲，后上梁山入伙"},
    {"nickName":"大  刀","heroName":"关胜","id":4,"level":30,"blood":0.88,"intro":"在梁山好汉中排名第五，位居马军五虎将第一位，河东解良（今山西运城）人，是三国名将关羽的后代，精通兵法，使一把青龙偃月刀"},
    {"nickName":"豹子头","heroName":"林冲","id":5,"level":30,"blood":0.85,"intro":"东京人氏，原是八十万禁军枪棒教头，因其妻子被太尉高俅的养子高衙内看上，而多次遭到陷害，最终被逼上梁山落草"},
    {"nickName":"霹雳火","heroName":"秦明","id":6,"level":30,"blood":0.83,"intro":"因其性如烈火，故而人称“霹雳火”。祖籍山后开州。善使一条狼牙棒。本是青州指挥司统制，攻打清风山时，因中宋江的计策，被俘后无家可归，只得归顺"},
    {"nickName":"双  鞭","heroName":"呼延灼","id":7,"level":30,"blood":0.82,"intro":"宋朝开国名将铁鞭王呼延赞嫡派子孙，祖籍并州太原（今属山西太原），上梁山之前为汝宁郡都统制，武艺高强，杀伐骁勇，有万夫不当之勇"},
    {"nickName":"小李广","heroName":"花荣","id":8,"level":30,"blood":0.81,"intro":"原是清风寨副知寨，使一杆银枪，一张弓射遍天下无敌手，生得一双俊目，齿白唇红，眉飞入鬓，细腰乍臂，银盔银甲，善骑烈马，能开硬弓"},
    {"nickName":"小旋风","heroName":"柴进","id":9,"level":30,"blood":0.79,"intro":"沧州人氏，后周皇裔，人称柴大官人。他曾帮助过林冲、宋江、武松等人，仗义疏财，后因李逵在高唐州打死殷天锡，被高廉打入死牢，最终被梁山好汉救出，因此入伙梁山"}
];
cc.Class({
    extends: cc.Component,

    properties: {
        contentView: {
            type: cc.ScrollView,
            default: null,
        },
        // cell prefab
        heroPrefab: {
            type: cc.Prefab,
            default: null,
        },
        // show hero introduction
        heroInfo: {
            type: cc.Node,
            default: null,
        },
        btnClose: {
            type: cc.Button,
            default: null,
        },
        heroPhoto: {
            type: cc.Sprite,
            default: null,
        },
        level: {
            type: cc.Label,
            default: null,
        },
        nickName: {
            type: cc.Label,
            default: null,
        },
        heroName: {
            type: cc.Label,
            default: null,
        },
        // 英雄介绍
        intro: {
            type: cc.Label,
            default: null,
        },
        // 写真集
        photoSprite: {
            type: [cc.SpriteFrame],
            default: [],
        }
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        // 隐藏信息窗口
        this.heroInfo.active = false;
        for(let i=0; i< HeroInfoData.length; ++i) {
            const heroData = HeroInfoData[i];
            const ins = cc.instantiate(this.heroPrefab);
            ins.setPosition(cc.v2(0, -i*ins.height));
            // register click event
            const clickEvent = new cc.Component.EventHandler();
            // 显示英雄介绍
            // 处理点击事件的节点
            clickEvent.target = this;
            // 本脚本名字
            clickEvent.component = 'heroList';
            clickEvent.handler = 'showHeroInfo';
            clickEvent.customEventData = i.toString();
            const btn = ins.getChildByName('head').getComponent(cc.Button);
            btn.clickEvents.push(clickEvent);

            const cell = ins.getComponent('heroCell');
            this.contentView.content.addChild(ins);
            // content 高度需要大于view高度，才能滚动
            this.contentView.content.height = (i+1)*ins.height;
            cell.updateHeroCell(i, heroData.nickName, heroData.heroName, heroData.blood);
        }
    },
    showHeroInfo(event, customEventData) {
        if(this.heroInfo.active) {
            // 已经显示了一个英雄，屏蔽点击
            return;
        }
        // 获取显示英雄的脚本
        const heroData = HeroInfoData[parseInt(customEventData)];
        if(heroData) {
            this.heroPhoto.spriteFrame = this.photoSprite[parseInt(customEventData)];
            this.level.string = heroData.level;
            this.nickName.string = heroData.nickName;
            this.heroName.string = heroData.heroName;
            this.intro.string = heroData.intro;
            this.heroInfo.active = true;
        }
    },

    showHeroList() {
        this.heroInfo.active = false;
    }

    //
    // start () {
    //
    // },

    // update (dt) {},
});
